home *** CD-ROM | disk | FTP | other *** search
/ Belgian Amiga Club - ADF Collection / BS1 part 34.zip / BS1 part 34 / FredFish PD 314.adf / Zc / zcsrc.lzh / genstubs / gettok.c < prev    next >
C/C++ Source or Header  |  1988-06-18  |  3KB  |  164 lines

  1. /*
  2.  * gettok: get next token from the input stream.
  3.  *
  4.  * Copyright 1988 by J.A. Lydiatt.  Permission is hereby given to 
  5.  * freely distribute this code for non-commercial use.
  6.  * Maintenance Notes:
  7.  *   18Jun88 - V1.0 Created by Jal.
  8.  *
  9.  */
  10.  
  11. #include "genstubs.h"
  12. #include <ctype.h>
  13.  
  14. #define MAXLINE        128
  15.  
  16. typedef struct lineinfo {
  17.     int  next;
  18.     int  length;
  19.     char line[MAXLINE];
  20.     } LINEINFO;
  21.  
  22.  
  23. /*
  24.  * External variables:
  25.  */
  26.  
  27. extern FILE *fi;
  28.  
  29. static LINEINFO curline;
  30. static int nextchar;
  31.  
  32. void nextline()
  33. {
  34.     register LINEINFO *l = &curline;
  35.     register char *p;
  36.     extern char *fgets();
  37.     extern int strlen();
  38.  
  39.     p = fgets( l->line, MAXLINE, fi);
  40.     if ( p == NULL )
  41.         l->line[0] = '\0';
  42.  
  43.     l->length = strlen( l->line );
  44.     l->next = 0;
  45.     nextchar = l->length == 0 ? EOF : l->line[ l->next++ ];  
  46. }
  47.  
  48. /*
  49.  * getcurline: return the current line in line.
  50.  */
  51.  
  52. void getcurline( line )
  53. char *line;
  54. {
  55.     register char *p;
  56.     register char *q;
  57.  
  58.     for ( p=line, q=curline.line; q && *q != '\n'; ++p,++q )
  59.         *p = *q;
  60.     *p = '\0';
  61. }
  62.  
  63. /*
  64.  * warn: prints the current line followed by the warning message.
  65.  */
  66.  
  67. void warn( msg )
  68. char *msg;
  69. {
  70.     extern int fputs();
  71.  
  72.     (void) fprintf( stderr, "%s", curline.line );
  73.     (void) fputs( msg, stderr );
  74.     (void) fputs( "\n\n", stderr );
  75. }
  76.  
  77. /*
  78.  * gnc: get next character from the input stream.
  79.  */
  80.  
  81. static int gnc()
  82. {
  83.     register LINEINFO *l = &curline;
  84.  
  85.     if ( l->next >= l->length )
  86.         if ( l->length > 0 )
  87.             nextline();    
  88.         else
  89.             return EOF;
  90.     else
  91.         nextchar = l->line[ l->next++ ];  
  92.     return nextchar;
  93. }
  94.  
  95. /*
  96.  * getid: get a variable name from the input file.
  97.  */
  98.  
  99. static void getid( t )
  100. register TOKEN *t;
  101. {
  102.     int i = 0;
  103.  
  104.     while ( (isalpha(nextchar) || isdigit(nextchar) || nextchar == '_')
  105.          && i < MAXSTR-1 ){
  106.         t->id[ i++ ] = nextchar;
  107.         (void) gnc();
  108.     }
  109.     t->id[ i ] = '\0';
  110. }
  111.  
  112. /*
  113.  * gettok: get the next token from the current line.
  114.  */
  115.  
  116. int gettok( t )
  117. register TOKEN *t;
  118. {
  119.     int value, i;
  120.  
  121.     while ( nextchar == ' ' || nextchar == '\t' )
  122.         (void) gnc();
  123.  
  124.     if ( nextchar == EOF )
  125.         return EOF;
  126.  
  127.     if ( nextchar == '#' ){
  128.         if ( gnc() != '#' ){
  129.             t->value = nextchar;
  130.             (void) gnc();
  131.             return UNKNOWN;
  132.         } else {
  133.             (void) gnc();
  134.             getid( t );
  135.             return DIRECTIVE;
  136.         }
  137.     } else if ( isalpha(nextchar) || nextchar == '_' ){
  138.         getid( t );
  139.         return ID;
  140.  
  141.     } else if ( isdigit( nextchar ) ){
  142.         value = 0;
  143.         do {        
  144.             value = value * 10 + (nextchar - '0');
  145.             (void) gnc();
  146.         }
  147.            while ( isdigit( nextchar ) );
  148.         t->value = value;
  149.         return NUMBER;
  150.     } else {
  151.         t->value = nextchar;
  152.         (void) gnc();
  153.         switch( t->value ){
  154.             case '\n':return EOL;
  155.             case '(': return LPAREN;
  156.             case ')': return RPAREN;
  157.             case ',': return COMMA;
  158.             case '/': return SLASH;
  159.             case '*': return COMMENT;
  160.             default:  return UNKNOWN;
  161.         }
  162.     }
  163. }
  164.